<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	<title>菜单管理</title>
	<link rel="stylesheet" type="text/css" media="screen" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/blue/main.css"/>" />
</head>
<body>
	<s:form name="pageForm" theme="simple">
		<div class="tabBlock">
			<s:iterator value="page.data" id="menuType" status="menuStatus" var="menuType">
			<div class="editBlock">
				<s:property value="name"/>
				<table width="100%" cellpadding="0" cellspacing="0">
					<tr>
						<th style="text-align: center;">
							序号/编号
						</th>
						<th style="text-align: center;">
							主菜单
						</th>
						<th style="text-align: center;">
							菜单项
						</th>
						<th style="text-align: center;">
							功能描述
						</th>
						<th style="text-align: center;">
							所需权限
						</th>
						<th style="text-align: center;">
							操作
						</th>
					</tr>
					<s:iterator value="menus" id="module" status="moduleStatus">
						<!-- 主菜单项遍历 -->
						<s:if test="menus.size==0">
							<!-- 无子菜单项 -->
						</s:if>
						<s:else>
							<!-- 有子菜单项 -->
							<s:iterator value="menus" id="funcPurview" status="funcStatus">
								<!-- 子菜单项遍历 -->
								<tr>
									<td>
										<s:property value="#moduleStatus.index+1" />-<s:property value="#funcStatus.index+1" />
									</td>
									<!-- 第二列，每个模块首行有个模块名称列，跨多行 -->
									<s:if test="#funcStatus.index==0">
										<td rowspan="<s:property value="#module.menus.size+#module.menus.size+1"/>">
											<input type="text" id="edit_order_no_<s:property value="#module.id"/>" value="<s:property value="#module.orderNo"/>" /><br>
											<input type="text" id="edit_name_<s:property value="#module.id"/>" value="<s:property value="#module.name"/>" />
										</td>
									</s:if>
									<!-- 第三列，菜单名称列 -->
									<td rowspan="<s:property value="2"/>">
										<input type="text" id="edit_order_no_<s:property value="id"/>" value="<s:property value="orderNo"/>" /><br>
										<input type="text" id="edit_name_<s:property value="id"/>" value="<s:property value="name"/>" />
										<s:if test="icon!=null">
											<img alt="<s:property value="name"/>" src="<s:property value="icon"/>">
										</s:if>
									</td>
									<td>
										菜单: <input type="text" name="name" value="<s:property value="name"/>" />
										标识:<input type="text" name="url" value="<s:property value="url"/>" /><br/>
										描述: <input type="text" name="description" value="<s:property value="description"/>" />
									</td>
									<td>
										<s:property value="purview.name"/><a onclick="selectPurview(null);">选择权限</a>
									</td>
									<td class="operate">
										<a onclick="del(this, <s:property value="id"/>, <s:property value="level"/>,'<s:property value="name"/>');">删除</a>
										<a onclick="save(this, <s:property value="id"/>, <s:property value="#funcPurview.id"/>);">保存</a>
										<select onchange="changeOrder(<s:property value="id"/>,this.value)">
											<option value="-2">上移2行</option>
											<option value="-1">上移1行</option>
											<option value="" selected="selected">移动</option>
											<option value="1">下移1行</option>
											<option value="2">下移2行</option>
										</select>
									</td>
								</tr>
								<!-- 菜单 -->
								<tr id="<s:property value="#moduleStatus.index+1" />-<s:property value="#funcStatus.index+1" />-*">
									<td>
										<s:property value="#moduleStatus.index+1" />-<s:property value="#funcStatus.index+1" />-*
									</td>
									<td>
										(新建下级菜单项，不需要)
									</td>
									<td>
										<a onclick="selectPurview(null);">选择权限</a>
									</td>
									<td class="operate">
										<a onclick="save(this, null, <s:property value="#funcPurview.id"/>);">保存新建下级菜单</a>
									</td>
								</tr>
								<!-- 子菜单项遍历 结束 -->
							</s:iterator>
						</s:else>

						<!-- 添加子菜单项 -->
						<tr>
							<td>
								<s:property value="#moduleStatus.index+1" />-*
							</td>
							<s:if test="#module.menus.size==0">
								<td
									rowspan="<s:property value="1"/>">
									<input type="checkbox" id='<s:property value="id"/>'
										value="<s:property value="id"/>"
										onchange="moduleChange(this)" onClick="this.blur();"
										<s:if test="authorized"> checked="checked"</s:if> />
									<label>
										<s:property value="name" />
									</label>
								</td>
							</s:if>
							<td colspan="2">
								新建菜单: <input type="text" id="edit_name_<s:property value="#module.id"/>" name="name" />
								标识:<input type="text" id="edit_url_<s:property value="#module.id"/>" name="url"  /><br/>
								功能描述: <input type="text" id="edit_desc_<s:property value="#module.id"/>" name="description"  />
							</td>
						<td>
							<a onclick="selectPurview(null);">选择权限</a>
						</td>
							<td class="operate">
								<a onclick="save(this, null, <s:property value="#module.id"/>);">保存新建菜单</a>
							</td>
						</tr>
						<!-- 模块遍历 结束 -->
					</s:iterator>
					<!-- 添加模块 -->
					<tr>
						<td>
							*
						</td>
						<td colspan="3">
								新建功能模块:<input type="text" name="name" />
								标识:<input type="text" name="url" />
								描述:<input type="text" name="description" />
						</td>
						<td>
							<a onclick="selectPurview(<s:property value="#menuType.pkId"/>);">选择权限</a>
						</td>
						<td class="operate">
							<a onclick="save(this, null, <s:property value="id"/>);">保存新建模块</a>
						</td>
					</tr>
				</table>
			</div>
			</s:iterator>
			<div class="toolbar" style="text-align: center;">
				<a onclick="pageForm.submit();" style="text-decoration: none;">刷新</a>
				<a onclick="history.go(-1);" style="text-decoration: none;">返回</a>
			</div>
		</div>
	</s:form>
	<script type="text/javascript" src="../js/jquery.js"></script>
	<script type="text/javascript" src="../js/store.js"></script>
	<script type="text/javascript">
		// 模块选择改变
		function moduleChange(obj) {
			if (!obj.checked) {
				$(obj).parent().parent().parent().next().find("table :checkbox").attr(
						"checked", false);
			} else {
				$(obj).parent().parent().parent().next().find("table :checkbox").attr(
						"checked", true);
			}
		}
		
		// 更新模块选择状态
		function updateParent(obj, moduleId) {
			if (obj.checked) {
				$("#" + moduleId).attr("checked", true);
			} else {
				var rdoValue = $("#radio_" + obj.id);
				if (rdoValue) {
					$("#" + rdoValue.val()).attr("checked", false);
					rdoValue.attr("disabled", true);
				}
			}
		}
		
		function rdoChange(obj, rdoId) {
			if (obj.checked) {
				var rdoValue = $("#radio_" + rdoId);
				rdoValue.attr("disabled", false);
				rdoValue.val(obj.value);
				$("#" + rdoId).attr("checked", true);
			}
		}
		
		function del(_this, id, level, name) {
			var objName;
			switch (level) {
			case 1:
				objName = "主菜模块";
				break;
			default:
				objName = "子菜单项";
				break;
			}
			if (confirm("真的要删除该"+objName+" "+name+" 吗?")){
				$.ajax({
					url: "menu-del.action",
					type: "POST",
					data: {"item.id":id},
					dataType: "json",
					success: function(result){
						if(result.success){
							alert("删除成功！");
							$("form")[0].submit();
						}else{
							alert("删除失败："+result.errorMessage);
						}
					}
				});
			}
		}
		
		function cancel(aElement){
			$("tr#"+aElement).hide();
			alert("aa");
		}
		
		function save(_this, id, parentId){
			var tr = $(_this).parent().parent();
			var name = $(tr).find("input[name='name']").val();
			var url = $(tr).find("input[name='url']").val();
			var description = $(tr).find("input[name='description']").val();
			var purviewKey = $(tr).find("input[name='purviewKey']").val();
			var operateName;
			var data = {"item.parent.id":parentId, "item.name":name,"item.url":url,"item.description":description};
			if(id&&id>0){
				operateName = "保存";
				data["item.id"] = id;
			}else{
				operateName = "添加";
			}
			$.ajax({
				url: "menu-save.action",
				type: "POST",
				data: data,
				dataType: "json",
				success: function(result){
					if(result.success){
						alert(operateName+"成功！");
						$("form")[0].submit();
					}else{
						alert(operateName+"失败："+result.errorMessage);
					}
				}
			});
		}
		
		function changeOrder(purviewId, offset){
			var form = $("form")[0];
			form.action = "menuChangeOrder";
			loadFrom("item.id",purviewId,form);
			loadFrom("offset",offset,form);
		}
	</script>
</body>
</html>
